const app = getApp();
// pages/profileTab/courseDetail/courseDetail.js
Page({
  data: {
    flag: 0,
    score: "0.0",
    currentComment: '',
    comment: ["很差","较差","一般","较好","很好"],
    noteMaxLen: 150, // 最多放多少字
    info: "",
    noteNowLen: 0,//备注当前字数
    //课程信息
    courseType: '',
    classHour: '',
    totalMoney: null,
    hidenSub: false,//后台获取，是否已经评论
    hidenComm: true,//值与hidennSub相反
    commentData: "评论内容",//后台获取，评论内容
    commentDate: "2019/06/01 23:48",//后台获取，评论时间
    dataId:null
  },
  //加载页面
  onLoad: function (options) {
    var that = this;
     if (app.globalData.evaluted==1){
      this.setData({
        hidenSub:true,
        hidenComm:false,
        commentData :app.globalData.commentData,
        commentDate : app.globalData.commentDate
        // commentDate:123
      })
    }
    this.setData({
      dataId: options.dataId,
    })
    console.log(options.dataId);
    console.log(app.globalData.courseIndex,'app.globalData.courseIndex')
    
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/courseInf',
      method: 'GET',
      data: {
        orderid: this.data.dataId
      },
      success(res) {
        console.log(res.data)
        var data = res.data;
        that.setData({
          courseType: data.courseType,
          classHour: data.classHour,
          totalMoney: data.totalMoney
        })
        console.log(that.data.courseType,'that.data.courseType')
      }
    })
  },
  // 监听字数
  bindTextAreaChange: function (e) {
    console.log(e.detail)
    var that = this;
    var value = e.detail.value;
    console.log(value,'value');
    var len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({ 
      info: value, 
      noteNowLen: len 
    })
  },
  //获取评分
  getScore: function(e) {
    console.log(e.detail)
    this.setData({
      score: e.detail.value + ".0",
      flag: e.detail.value,
      currentComment: this.data.comment[e.detail.value - 1]
    })
  },
  //提交表单
  formSubmit: function(e) {
    console.log("提交表单成功")
    console.log(e.detail)

    var that = this;
    console.log(that.data.info,'that.data.info')

    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/evaluate',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        orderId: that.data.dataId,
        content: that.data.info,
        evaluateScore: that.data.score,
      }
    })

    wx.showToast({
      title: '发布成功',
      icon: 'success',
      duration: 1500,
      mask: false,
      success: function () {
        // 提交之后表单清空

        that.setData({
          // flag: 0,
          // score: "0.0",
          // currentComment: '',
          // noteNowLen: 0,
          // info: '',
          hidenSub: true, 
          hidenComm: false
        })
      }
    })
  },
  //查看评论
  showComm: function() {
    var that = this;
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/evaContent',
      method: 'GET',
      data: {
        orderid: that.data.dataId 
      },
      success(res) {
          console.log(res,'res'),
          app.globalData.commentData = res.data.data.content,
          app.globalData.commentDate = new Date(res.data.data.evaTime).toLocaleString(),
        that.setData({
          commentData: '',    
          commentDate: new Date(res.data.data.evaTime).toLocaleString(),
        })
        // console.log(commentData, 'commentData')
      }
    })
  }
})